
import BaseView from "../base/BaseView";
import PlateAmountItem from "../base/items/PlateAmountItem";
import TimeModule from "../module/TimeModule";
import VirtualList from "../ui/VirtualList";
import DropList from "./items/DropList";
import { PlateNames, ThsRankData } from "./models/ThsRankData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlateAmountView extends BaseView {

    @property(DropList)
    dropDate: DropList = null;

    @property(DropList)
    dropPlate: DropList = null;

    @property(DropList)
    dropRank: DropList = null;

    @property(VirtualList)
    amoutList: VirtualList<PlateAmountItem> = null;

    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    _tradeDates: any = [];

    _selectDateIndex: number = 0;

    // LIFE-CY-CLE CALLBACKS:

    onLoad () {
        let dates = TimeModule.getInstance().getRecentTradeDates(120);
        this._tradeDates = dates;
        this.dropDate.setData(dates, 0);
        this.dropPlate.setData(PlateNames, 0);
        this.dropRank.setData(['成交金额', '总市值', '换手率'], 0);
        this.dropPlate.setSelectCallback((index)=>{
            this.updateView();
        });
        this.dropDate.setSelectCallback((index)=>{
            this._selectDateIndex = index;
            this.updateView();
        });
        this.dropRank.setSelectCallback((index)=>{
            this.updateView();
        });
        this.updateView();
    }

    protected onEnable(): void {
        this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this, true);
        this.scrollview.node.off(cc.Node.EventType.MOUSE_WHEEL);
    }

    protected onDisable(): void {
        this.node.off(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this, true);
    }

    async updateView () {
        this.scrollview.scrollToTop();
        let date = this.dropDate.getSelectData();
        let type = this.dropPlate.getSelectIndex();
        let plateAmouts = await ThsRankData.getInstance().getPlateAmounts(type, date);
        let maxAmount = 0;
        for (let index = 0; index < plateAmouts.length; index++) {
            const item = plateAmouts[index];
            if (item.amount > maxAmount) {
                maxAmount = item.amount;
            }
        }
        for (let index = 0; index < plateAmouts.length; index++) {
            const item = plateAmouts[index];
            item.maxAmount = maxAmount;
        }
        let rankType = this.dropRank.getSelectIndex();
        let key = '';
        switch (rankType) {
            case 0:
                key = 'amount';
                break;
            case 1:
                key = 'total';
                break;
            case 2:
                key = 'rate';
                break;
            default:
                key = 'total';
                break;
        }
        
        plateAmouts.sort(function (a, b) {
            if (b[key] == null) {
                return -1000000000000;
            }
            if (a[key] == null) {
                return 1000000000000;
            }
            return b[key] - a[key];
        });

        this.amoutList.argsArr = plateAmouts;
    }

    onMouseWheel (event) {
        let y = event.getScrollY();
        if (y > 0) {
            this.onBtnUp();
        } else {
            this.onBtnDown();
        }
    }

    onBtnDown () {
        this._selectDateIndex++;
        if (this._selectDateIndex >= this._tradeDates.length) {
            this._selectDateIndex = this._tradeDates.length - 1;
        } else {
            this.dropDate.setSelectIndex(this._selectDateIndex);
            this.updateView();
        }
    }

    onBtnUp () {
        this._selectDateIndex--;
        if (this._selectDateIndex < 0) {
            this._selectDateIndex = 0;
        } else {
            this.dropDate.setSelectIndex(this._selectDateIndex);
            this.updateView();
        }
    }

    // update (dt) {}
}
